package com.jiaeda.shiromaster.service.imp;


import com.jiaeda.shiromaster.entity.SysPermission;
import com.jiaeda.shiromaster.entity.SysRole;
import com.jiaeda.shiromaster.entity.SysRolePermission;
import com.jiaeda.shiromaster.mapper.SysPermissionMapper;
import com.jiaeda.shiromaster.service.SysPermissionService;
import com.jiaeda.shiromaster.service.SysRolePermissionService;
import com.jiaeda.shiromaster.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by chengchaopeng on 2018/6/27.
 */
@Service
public class SysPermissionServiceImp implements SysPermissionService {

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Override
    public List<SysPermission> findByPermissionIds(String[] permissionIds) {
        return sysPermissionMapper.findByPermissionIds(permissionIds);
    }

    @Override
    public List<SysPermission> findByLoginAccount(String loginAccount) {
        //根据用户登录名，获取对应的角色信息
        List<SysRole> sysRoleList = sysRoleService.findByLoginAccount(loginAccount);
        //提取角色主键
        StringBuffer roleIds = new StringBuffer();
        sysRoleList.forEach(sysRole -> {
            roleIds.append(sysRole.getRoleId()).append(";");
        });
        //根据角色主键列表获取所对应的权限主键
        List<SysRolePermission> sysRolePermissionList = sysRolePermissionService.findByRoleIds(roleIds.toString().split(";"));
        //提取权限主键
        StringBuffer permissionIds = new StringBuffer();
        sysRolePermissionList.forEach(sysRolePermission -> {
            permissionIds.append(sysRolePermission.getPermissionId()).append(";");
        });
        return sysPermissionMapper.findByPermissionIds(permissionIds.toString().split(";"));
    }


}
